home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 July / macformat-026.iso / mac / Shareware City / Developers / video-toolbox-95-01-14-c / VideoToolbox / VideoToolboxSources / GetScreenDevice.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-11-30  |  6.5 KB  |  205 lines  |  [TEXT/KAHL]

  1. /* GetScreenDevice.c
  2. PROBLEM:
  3. How can I tell whether I've got a CGrafPtr or a GWorldPtr? (In one case I'll call 
  4. GetGWorldDevice, in the other case I'll get max device.)
  5. I noticed that the portVersions are 0xc001 and 0xc000. Should I use that to tell?
  6.  
  7. ANSWER FROM APPLE DEVSUPPORT (8/93):
  8. You can use GetGWorldDevice() on GWorldPtr, GrafPort or CGrafPort. Use
  9. TestDeviceAttibute() to test if the returned GDHandle is a "real" screen or not.
  10.  
  11. Also, you should refer to the tech note “RowBytes Revealed II” (available on the
  12. June Developer CD) which states:
  13.  
  14. You'll notice in Figure 1 that the portVersion field of a cGrafPort coincides
  15. with the location of the rowBytes field of a grafPort. Remember, a cGrafPort has
  16. the same size as a grafPort. During debugging, you can use the same information
  17. that CopyBits uses to identify cGrafPorts.
  18.  
  19. If you use a grafPort template to display memory for an unknown grafPort, you
  20. can tell if it is a cGrafPort because the rowBytes will be equal to 0xC000. The
  21. 0xC corresponds to the two high bits being set in the portVersion field of a
  22. cGrafPort. Since these bits can not be set in a grafPort, you know you have a
  23. cGrafPort. In addition, if the bottom bit of the portVersion field is set, then
  24. it is a gWorld. Thus, if your rowBytes field has a value of 0xC001, then you
  25. know you have a gWorld.
  26.  
  27. CONCLUSION:
  28. GetWindowDevice() assumes that it's a GWorld iff the bits 0xC001 of the
  29. are set. From Apple's comment above this is clearly true at present and probably
  30. safe for the future.
  31.  
  32. Copyright © 1989-1994 Denis G. Pelli
  33. HISTORY:
  34. 3/20/90        dgp    make compatible with MPW C.
  35. 3/22/90    dgp    changed GetDeviceSlot to use the AuxDCEHandle instead of deducing it
  36.             from the baseAddr of the PixMap. This is a cleaner way to do it.
  37. 4/9/90    dgp    eliminated #define for Mainscrn mispelling in Color.h
  38. 10/17/90 dgp Added AddressToScreenDevice() for compatibility with built-in video on
  39.             the Mac IIci, IIsi, and LC.
  40. 10/18/90 dgp Added LocalToGlobalRect() and GetWindowDevice().
  41. 8/24/91        dgp    Made compatible with THINK C 5.0.
  42. 2/1/92    dgp    Fixed bugs in GetWindowDevice() which resulted in returning garbage GDHandle.
  43. 3/3/92    dgp    In GetScreenDevice(), skip inactive screens.
  44. 8/20/92    dgp    expanded comments of GetDeviceSlot(), noting that it works even with
  45.             built-in video, e.g. on Mac IIci.
  46. 8/26/92    dgp    GetDeviceSlot() now returns -1 if none, since zero is a legal slot.
  47.             GetScreenDevice() first checks for 8-bit QuickDraw().
  48. 9/10/92    dgp    Actually implemented the 8/26 change instead of just changing the 
  49.             documentation. Oops.
  50. 4/17/93    dgp Deleted obsolete AddressToSlot and AddressToScreenDevice.
  51. 5/21/93    dgp    Fixed GetWindowDevice() to support GWorld's.
  52. 8/14/93    dgp    Based on answer from DEVSUPPORT, I cleaned up GetWindowDevice().
  53. 4/11/94    dgp    Added GetRectDevice() based on code extracted from GetWindowDevice().
  54. 11/30/94 dgp added GetDeviceByRefNum().
  55. */
  56. #include "VideoToolbox.h"
  57. GDHandle GetRectDevice(Rect *r);
  58. GDHandle GetDeviceByRefNum(int n);
  59.  
  60. GDHandle GetScreenDevice(int n)
  61. // Returns a handle to the n-th screen, where the MainDevice is the zero-th screen.
  62. // Returns NULL if request can't be satisfied.
  63. {
  64.     GDHandle device;
  65.     int i,error;
  66.     long value;
  67.  
  68.     if(n<0)return NULL;
  69.     error=Gestalt(gestaltQuickdrawVersion,&value);
  70.     if(error || value<gestalt8BitQD)return NULL;    // need 8-bit quickdraw
  71.     if(n==0) return GetMainDevice();
  72.     device=GetDeviceList();
  73.     i=0;
  74.     while(device!=NULL){
  75.         if (TestDeviceAttribute(device,screenDevice)
  76.             && !TestDeviceAttribute(device,mainScreen)
  77.             && TestDeviceAttribute(device,screenActive)){
  78.                 i++;
  79.                 if(i==n)break;
  80.             }
  81.         device = GetNextDevice(device);
  82.     }
  83.     return device;
  84. }
  85.  
  86. GDHandle GetDeviceByRefNum(int n)
  87. {
  88.     int j;
  89.     GDHandle device;
  90.  
  91.     for(j=0;;j++){
  92.         device=GetScreenDevice(j);
  93.         if(device==NULL || n==(**device).gdRefNum)break;
  94.     }
  95.     return device;
  96. }
  97.  
  98. int GetScreenIndex(GDHandle device)
  99. // Inverse of GetScreenDevice(). Returns -1 if request can't be satisfied.
  100. {
  101.     int i,error;
  102.     long value;
  103.  
  104.     error=Gestalt(gestaltQuickdrawVersion,&value);
  105.     if(error || value<gestalt8BitQD)return 0;
  106.     if(device==NULL)return -1;
  107.     for(i=0;i<16;i++)if(device==GetScreenDevice(i))return i;
  108.     return -1;
  109. }
  110.  
  111. short int GetDeviceSlot(GDHandle device)
  112. // Gets the "slot" for any screen device, even if it's built-in video, e.g. on Mac
  113. // IIci or Quadra. See 1992 Inside Mac "Processes" page 4-11. Returns -1 if none.
  114. // Zero is a legal slot for built-in video devices.
  115. {
  116.     AuxDCEHandle myAuxDCEHandle;
  117.  
  118.     if(device == NULL) return -1;
  119.     myAuxDCEHandle=(AuxDCEHandle) GetDCtlEntry((**device).gdRefNum);
  120.     return ((**myAuxDCEHandle).dCtlSlot);
  121. }
  122.  
  123. GDHandle SlotToScreenDevice(int n)
  124. // Returns a handle to the screen device in slot n.
  125. // Returns NULL if request can't be satisfied.
  126. {
  127.     GDHandle device;
  128.  
  129.     device=GetDeviceList();
  130.     while(device!=NULL) {
  131.         if (TestDeviceAttribute(device,screenDevice) &&
  132.             GetDeviceSlot(device)==n)
  133.                 break;
  134.         device=GetNextDevice(device);
  135.     }
  136.     return device;
  137. }
  138.  
  139. GDHandle GetWindowDevice(WindowPtr window)
  140. // For on-screen window, returns GDHandle of screen with largest intersection with the 
  141. // window's content.
  142. // For off-screen window (i.e. GWorld), it returns the GDHandle of the associated device.
  143. {
  144.     Rect r;
  145.     WindowPtr oldWindow;
  146.     long qD;
  147.  
  148.     if(window==NULL)return NULL;
  149.     Gestalt(gestaltQuickdrawVersion,&qD);
  150.     if(qD>=gestalt32BitQD && (((CWindowPtr)window)->portVersion&0xc001)==0xc001){
  151.         // It's a GWorld iff the portVersion has both high bits set (cGrafPort) and
  152.         // the low bit set (GWorld). See Apple Tech Note “RowBytes Revealed II”.
  153.         return GetGWorldDevice((GWorldPtr)window);
  154.     }
  155.     r=window->portRect;
  156.     GetPort(&oldWindow);
  157.     SetPort(window);
  158.     LocalToGlobalRect(&r);
  159.     SetPort(oldWindow);
  160.     return GetRectDevice(&r);
  161. }
  162.  
  163. GDHandle GetRectDevice(Rect *r)
  164. // Returns GDHandle of screen with largest intersection with the global rect.
  165. {
  166.     Rect overlap;
  167.     GDHandle device,dominantDevice=NULL;
  168.     long area,greatestArea;
  169.     long qD;
  170.  
  171.     if(r==NULL)return NULL;
  172.     Gestalt(gestaltQuickdrawVersion,&qD);
  173.     if(qD<gestalt8BitQD)return NULL;    // need 8-bit quickdraw
  174.     device=GetDeviceList();
  175.     greatestArea=0;
  176.     while(device!=NULL){
  177.         if(TestDeviceAttribute(device,screenDevice)
  178.             && TestDeviceAttribute(device,screenActive)){
  179.                 SectRect(r,&(*device)->gdRect,&overlap);
  180.                 area=(long)(overlap.right-overlap.left)*(overlap.bottom-overlap.top);
  181.                 if(area>greatestArea){
  182.                     greatestArea=area;
  183.                     dominantDevice=device;
  184.                 }
  185.             }
  186.         device=GetNextDevice(device);
  187.     }
  188.     return dominantDevice;
  189. }
  190.  
  191. void LocalToGlobalRect(Rect *r)
  192. {
  193.     Point pt={0,0};
  194.     
  195.     LocalToGlobal(&pt);
  196.     OffsetRect(r,pt.h,pt.v);
  197. }
  198.  
  199. void GlobalToLocalRect(Rect *r)
  200. {
  201.     Point pt={0,0};
  202.     
  203.     GlobalToLocal(&pt);
  204.     OffsetRect(r,pt.h,pt.v);
  205. }